<?php
// +----------------------------------------------------------------------
// Csmmeet [ Csmonline ]
// Author: chensm <chenshiming0802@163.com>
// Create by chensm at 2020-03-31
// +----------------------------------------------------------------------
namespace app\admin\controller\csmonline;

use addons\csmonline\library\CsmBackend;
use addons\csmonline\library\CsmonlineCron;

/**
 * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmonline/chartonline/index
 */
class Chartonline extends CsmBackend
{

    /**
     */
    protected $model = null;

    public function index()
    {
        //1.0.2 修复了HTTPS无法打开地图的问题@chensm
        //header("Content-Security-Policy: upgrade-insecure-requests");
        // $isrefresh = $this->csmreq("isrefresh", false);
        $today = date("Y-m-d");
        $yestoday = date("Y-m-d", strtotime("-1 day"));

        // 是否强制刷新
        if (true) {
            CsmonlineCron::crononedate($today, true);
            $this->view->assign("baseurl", $this->request->baseUrl());
            //CsmonlineCron::crononedate($yestoday, false);
        }

        // 统计截止时间
        if (true) {
            $dao = new \app\admin\model\csmonline\Crontab();
            $row = $dao->where("statdate", "=", $today)->find();

            $this->view->assign("todaystat", ($row == null) ? "今天无数据" : date("Y-m-d H:i:s", $row->starttime));
        }

        // 游客统计 和 登录用户统计
        if (true) {
            $dao = new \app\admin\model\csmonline\Piestatdate();
            $todayrow = $dao->where("statdate", "=", $today)->find();
            $yestodayrow = $dao->where("statdate", "=", $yestoday)->find();
            $this->view->assign("today", $todayrow);
            $this->view->assign("yestoday", $yestodayrow);
        }
        // 在线人数,超过了心跳时间*2，则认为离线（有一次心跳没记录到所以*2，网络延迟所以+10秒）
        if (true) {
            $config = get_addon_config("csmonline");
            $heartseconds = (int) $config["heartseconds"];

            $firstid = CsmonlineCron::getUserLogFirstId($today);
            $daolog = new \app\admin\model\csmonline\Userlog();
            $onlinen = $daolog->where("id", ">=", $firstid)
                ->where("isloginuser", "=", "N")
                ->where("durationlasttime", ">", time() - 2 * $heartseconds - 10)
                ->count("distinct userid");
            // $this->tracedao($dao);
            $onliney = $daolog->where("id", ">=", $firstid)
                ->where("isloginuser", "=", "Y")
                ->where("durationlasttime", ">", time() - 2 * $heartseconds - 10)
                ->count("distinct userid");

            $this->view->assign("onlinen", $onlinen);
            $this->view->assign("onliney", $onliney);
        }

        return $this->view->fetch();
    }

    public function chart2()
    {
        $begin = $this->csmreq("begin", true);
        $end = $this->csmreq("end", true);
        // $dao = new \app\admin\model\csmonline\Piestatdate();
        // $row = $dao->where("statdate", ">=", $begin)
        // ->where("statdate", "<=", $end)
        // ->field("sum(pcusy) pcusy,sum(mobileusy) mobileusy,sum(pcusn) pcusn,sum(mobileusn) mobileusn")
        // ->find();
        $dao = new \app\admin\model\csmonline\Pieevn();
        $rowmobile = $dao->alias('t')
            ->where("statdate", ">=", $begin)
            ->where("statdate", "<=", $end)
            ->where("envvalue", "=", "手机")
            ->field("sum(uvsy) uvsy,sum(uvsn) uvsn")
            ->find();
        $rowpc = $dao->alias('t')
            ->where("statdate", ">=", $begin)
            ->where("statdate", "<=", $end)
            ->where("envvalue", "=", "PC电脑")
            ->field("sum(uvsy) uvsy,sum(uvsn) uvsn")
            ->find();

        $this->success("", null, array(
            "all" => [
                [
                    'name' => 'PC用户',
                    'value' => $rowpc->uvsn + $rowpc->uvsy
                ],
                [
                    'name' => '移动用户',
                    'value' => $rowmobile->uvsn + $rowmobile->uvsy
                ]
            ],
            "isloginusern" => [
                [
                    'name' => 'PC用户',
                    'value' => $rowpc->uvsn
                ],
                [
                    'name' => '移动用户',
                    'value' => $rowmobile->uvsn
                ]
            ],
            "isloginusery" => [
                [
                    'name' => 'PC用户',
                    'value' => $rowpc->uvsy
                ],
                [
                    'name' => '移动用户',
                    'value' => $rowmobile->uvsy
                ]
            ]
        ));
    }

    private function adjust($list)
    {
        $ll = [];
        foreach ($list as $v) {
            $ll[] = $v;
        }
        array_unshift($ll, [
            'product',
            '游客统计',
            '登录用户统计'
        ]);
        return $ll;
    }
}

